package com.baizhi.shane.sys.service.impl;

import com.baizhi.shane.sys.entity.SysDictValue;
import com.baizhi.shane.sys.manager.UserManager;
import com.baizhi.shane.sys.service.SysDictValueService;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baizhi.shane.sys.vo.InsertOneKeyInVO;
import com.baizhi.shane.sys.vo.PageKeyInVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.sys.entity.SysDictKey;
import com.baizhi.shane.sys.service.SysDictKeyService;
import com.baizhi.shane.sys.mapper.SysDictKeyMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author wbz12
* @description 针对表【sys_dict_key(数据字典key表)】的数据库操作Service实现
* @createDate 2023-03-26 18:19:03
*/
@Service
public class SysDictKeyServiceImpl extends ServiceImpl<SysDictKeyMapper, SysDictKey>
    implements SysDictKeyService{

    @Resource
    private UserManager userManager;
    @Resource
    private CommonUtils commonUtils;
    @Resource
    private SysDictValueService sysDictValueService;

    /**
     * 插入一条数据字典的KEY
     *
     * @Author 闻柏智
     * @Date 14:32 2023/4/30
     * @Param insertOneKeyInVO
     * @return java.lang.String
     **/
    @Override
    public String insertOneKey(InsertOneKeyInVO insertOneKeyInVO) {
        /* 提取变量 */
        String name = insertOneKeyInVO.getName();
        String note = insertOneKeyInVO.getNote();
        /* 构建插入对象 */
        SysDictKey sysDictKey = new SysDictKey();
        sysDictKey.setName(name);
        sysDictKey.setNote(note);
        sysDictKey.setCreateBy(userManager.getLoginUserId());
        // 插入
        boolean success = this.save(sysDictKey);
        String successStr = commonUtils.boolToString(success);
        return successStr;
    }

    /**
     * 分页查询所有字典Key
     *
     * @Author 闻柏智
     * @Date 19:48 2023/4/30
     * @Param pageKeyInVO
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.baizhi.baiweiexam.sys.entity.SysDictKey>
     **/
    @Override
    public Page<SysDictKey> pageKey(PageKeyInVO pageKeyInVO) {
        /* 提取变量 */
        Integer pageSize = pageKeyInVO.getPageSize();
        Integer pageNo = pageKeyInVO.getPageNo();
        /* 设置名称模糊查询 */
        String name = pageKeyInVO.getName();
        pageKeyInVO.setName(null);
        /* 查询 */
        LambdaQueryWrapper<SysDictKey> qwSdk = new LambdaQueryWrapper<>(pageKeyInVO);
        qwSdk.like(SysDictKey::getName,name);
        Page<SysDictKey> page = new Page<>(pageNo,pageSize);
        Page<SysDictKey> sysDictKeyPage = this.page(page, qwSdk);
        return sysDictKeyPage;
    }

    @Override
    public String insertOneValue(SysDictValue sysDictValue) {
        if (StringUtils.isAnyEmpty(
                sysDictValue.getValue(),
                sysDictValue.getText()) && sysDictValue.getKeyId() != null) {
            throw new RuntimeException("value、text、keyId不能有空值");
        }
        sysDictValue.setCreateBy(userManager.getLoginUserId());
        boolean success = sysDictValueService.save(sysDictValue);
        String successString = commonUtils.boolToString(success);
        return successString;
    }
}




